﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lucene.Net.Analysis;
using Lucene.Net.Store;
using Smallerp.Contracts.DataContracts;
using SmallERP.COM;

namespace SmallERP.PinBan
{
    public partial class FrmAssignDxf : XtraForm
    {
        public FrmAssignDxf(PhotoInfo_PBDCT inPbdctPbdct)
        {
            InitializeComponent();
            //WriteLogForCheck.WriteLogFile("传入" + inPbdctPbdct.PicPath);
            _photoInfoPbdct = inPbdctPbdct;
            _currPath = inPbdctPbdct.Photo_Base_Info.PicPath;

        }
        PhotoInfo_PBDCT  _photoInfoPbdct = new PhotoInfo_PBDCT();
        private string _currPath { get; set; }
        private  List<string> FileNamelist = new List<string>();
        public string SelectPath { get; set; }

        private void buttonEditSelectPath_EditValueChanged(object sender, EventArgs e)
        {

        }                                                                   
        private List<string> GetDxfFile(string inPath)
        {
            List<string> rtnList = new List<string>();
            if (System.IO.Directory.Exists(inPath))
            {
                rtnList = System.IO.Directory.GetFiles(inPath, "*.dxf").ToList();
            }
            else
            {
                return new List<string>();
            }
            return rtnList;
        }
        private void buttonEditSelectPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog=new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = IniFile.DxfFolderPath;
            folderBrowserDialog.Description = "选择DXF文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                IniFile.DxfFolderPath = folderBrowserDialog.SelectedPath;
            }
            this.buttonEditSelectPath.Text = IniFile.DxfFolderPath;
            FileNamelist=GetDxfFile(IniFile.DxfFolderPath);
            BindList();
        }

        
        private void FrmAssignDxf_Load(object sender, EventArgs e)
        {
            try
            {
                //WriteLogForCheck.WriteLogFile("获取到" + IniFile.DxfFolderPath);
           
            if(string.IsNullOrEmpty(IniFile.DxfFolderPath)  )
            {
                return;
            }
        
            FileNamelist = GetDxfFile(IniFile.DxfFolderPath);
           // WriteLogForCheck.WriteLogFile("获取到"+FileNamelist.Count());
            //GetDxfFile(IniFile.DxfFolderPath);
        
            BindList();
            labelControlInfo.Text = _photoInfoPbdct.Photo_Base_Info.PicPath;
            if (FileNamelist.Count > 0)
            {
                listBoxControldxfList.SelectedIndex = 0;
            }
            }
            catch (Exception ex)
            {

                WriteLogForCheck.WriteLogFile(ex.ToString());
            }
        }

        private void BindList()
        {
            if (FileNamelist.Count > 0)
            {
                FileNamelist = OrderDxfItem(FileNamelist, _currPath);
            }
            //WriteLogForCheck.WriteLogFile("分析完" + FileNamelist.Count());
              listBoxControldxfList.Items.Clear();
             FileNamelist.ForEach(ea =>
             { 
                 listBoxControldxfList.Items.Add(ea.ToString());
             });
            listBoxControldxfList.Items.Add("清除");
        }

        private List<string> OrderDxfItem(List<string> inDirList,string inStandStr)
        {
             List<string> rl=new List<string>();
            Dictionary<string,int>resultDict=new Dictionary<string, int>();
            List<string> standList = AnalyserStr(inStandStr);
            foreach (string dirStr in inDirList)
            {
                List<string> dirList = AnalyserStr(dirStr);
               var r= dirList.Intersect(standList);
               resultDict.Add(dirStr,r.Count());
            }
          var rd=  resultDict.OrderByDescending(or => or.Value);
            foreach (KeyValuePair<string, int> keyValuePair in rd)
            {
                    rl.Add(keyValuePair.Key);
            }
            return rl;
        }

        private List<string> AnalyserStr(string instr)
        {
             List<string> rtList=new List<string>();
            Analyzer analyzer = new Lucene.China.ChineseAnalyzer();
            StringReader sr = new StringReader(instr);
            TokenStream stream = analyzer.TokenStream(null, sr);

            Token t = stream.Next();
            while (t != null)
            {
                string t1 = "";

                t1 = t.ToString(); //显示格式： (字符,0,2) ，需要处理 
                t1 = t1.Replace("(", "");
                char[] separator = { ',' };
                var tep = t1.Split(separator);
                if (tep.Count() > 0)
                {
                    rtList.Add(tep[0]);
                }
                t = stream.Next();



            }
            return rtList;


        }

        private void simpleButtonSelect_Click(object sender, EventArgs e)
        {
            GetSelectItem();
        }

        private void listBoxControldxfList_DoubleClick(object sender, EventArgs e)
        {
            GetSelectItem();
        }

        private void  GetSelectItem()
        {
            var selectItem = listBoxControldxfList.SelectedItem;
            if (selectItem != null)
            {
                string slStr = selectItem.ToString();
                if (!"清除".Equals(slStr))
                {
                    SelectPath = slStr;
                    byte[] data;
                    using (FileStream fs = File.OpenRead(SelectPath))
                    {
                        data = new byte[fs.Length];
                        fs.Read(data, 0, data.Length);
                    }
                    _photoInfoPbdct.Photo_Base_Info.DXFContent = data;
                    _photoInfoPbdct.Photo_Base_Info.DXFFilePath = SelectPath;
                }
                else
                {
                    _photoInfoPbdct.Photo_Base_Info.DXFContent =new byte[1];
                    _photoInfoPbdct.Photo_Base_Info.DXFFilePath = "";
                }
                this.DialogResult = DialogResult.OK;
            }
            this.Close();
        }

        private void simpleButtonCacel_Click(object sender, EventArgs e)
        {
            this.Close();
        }





    }
}
